import React from "react";
import {Redirect} from "react-router-dom";
import {Result, Button, WhiteSpace, List, Modal} from "antd-mobile";
import {connect} from "react-redux";
import Cookies from "js-cookie";
import {logout} from "../../redux/user.reducer";

@connect(
  state => state,
  {logout}
)
class User extends React.Component {
  constructor () {
    super();
    this.doLogout = this.doLogout.bind(this);
  }

  doLogout () {
    const alert = Modal.alert;
    const prop = this.props;
    alert("提示", "确定要退出登录吗???", [
      {text: "取消"},
      {
        text: "确定", onPress: () => {
          Cookies.remove("userId");
          prop.logout();
        }
      },
    ]);
  }

  render () {
    const u = this.props.user;
    const Item = List.Item;
    const Brief = Item.Brief;

    return u.avatar ? (
      <div>
        <Result
          title={u.username}
          imgUrl={require(`../../component/user-card/img/${u.avatar}.png`)}
          message={u.role === "boss" ? u.company : u.job}/>
        <WhiteSpace/>
        <List>
          <Item multipleLine wrap={true}>
            {u.role === "boss" ? "职位要求:" : "自我评价:"}
            <Brief>{u.desc}</Brief>
          </Item>
          <Item>
            {u.role === "boss" ? "薪资范围:" : "薪资要求:"}
            <Brief>{u.salary}</Brief>
          </Item>
        </List>
        <WhiteSpace/>
        <WhiteSpace/>
        <WhiteSpace/>
        <Button type="warning" onClick={() => this.doLogout()}>退出登录</Button>
      </div>
    ) : <Redirect to={u.redirectTo}/>;
  }

}

export default User;